package com.thtn.cumtuthongdung;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HTMLParserCumtuEg2 {

	public static void main(String[] args) {
		StringBuffer stbEN = new StringBuffer();
		StringBuffer stbVN = new StringBuffer();

		StringBuffer stbKethop = new StringBuffer();
		Document doc;
		String link;
		try {
			for (int i = 1; i < 2; i++) {
				// link:
				// http://tratu.vietgle.vn/hoc-tieng-anh/cau-truc-cau-tieng-anh-thong-dung/vietgle-tra-tu/tat-ca/trang-1.html

				link = "http://tratu.vietgle.vn/hoc-tieng-anh/cau-truc-cau-tieng-anh-thong-dung/vietgle-tra-tu/tat-ca/trang-"
						+ i + ".html";
				System.out.println(link);
				doc = Jsoup.connect(link).get();

				// muc tu english: .ctk[id^=sp_originsen]
				// muc tu vietnamese: .cB[id^=sp_mean]
				// english examples: .ctk[id^=spsample]
				// vietnamese example: .p10l[id^=spmeansample]
				Elements enWord = doc.select(".ctk[id^=sp_originsen]");
				Elements vnWord = doc.select(".cB[id^=sp_mean]");
				Elements enExample = doc.select(".ctk[id^=spsample]");
				Elements vnExample = doc.select(".p10l[id^=spmeansample]");

				System.out.println("enWord = " + enWord.size());
				System.out.println("vnWord = " + vnWord.size());
				System.out.println("enExample = " + enExample.size());
				System.out.println("vnExample = " + vnExample.size());

				Elements enExample_eg1 = null;
				Elements vnExample_eg1 = null;
				ArrayList<String> listenExample_eg1 = new ArrayList<String>();
				ArrayList<String> listvnExample_eg1 = new ArrayList<String>();

				for (Element elemEnWord : enWord) {
					System.out.println(elemEnWord.id());
					System.out.println(elemEnWord.id().substring(12));
					String wordId = elemEnWord.id().substring(12);
					enExample_eg1 = doc.select(".ctk[id^=spsample_" + wordId
							+ "1]");
					vnExample_eg1 = doc.select(".p10l[id^=spmeansample_"
							+ wordId + "1]");

					System.out.println("Eg1_en = "
							+ enExample_eg1.get(0).text());
					System.out.println("Eg1_vn = "
							+ vnExample_eg1.get(0).text());

					listenExample_eg1.add(enExample_eg1.get(0).text());
					listvnExample_eg1.add(vnExample_eg1.get(0).text());

				}

				System.out.println(listenExample_eg1.get(0));
				for (int j = 0; j < enWord.size(); j++) {
					stbKethop.append(enWord.get(j).text() + "\t");
					stbKethop.append(vnWord.get(j).text() + "\t");
					stbKethop.append(listenExample_eg1.get(j) + "\t");
					stbKethop.append(listvnExample_eg1.get(j) + "\n");

				}

				// select using jquery .ctk[id^=sp_originsen]
				// Elements byClass = doc.select("[id^=sp]");
				// Elements byClass = doc.getElementsByClass("ctk");
				// Elements byClass_vi = doc.getElementsByClass("p10l");
				// System.out
				// .println("So luong cau tieng anh = " + byClass.size());
				// System.out.println("So luong cau tieng viet = "
				// + byClass_vi.size());
				// System.out.println("line Sep =" + System.getProperty("line.")
				// + "line Sep =");
				// System.out.println("tab" + "\t" + "tab");
				// for (Element byClas : byClass) {
				// // System.out.println(byClas.ownText());
				// stbEN.append(byClas.ownText());
				// stbEN.append("\t");
				// }
				// for (Element byClass_vi_e : byClass_vi) {
				// // System.out.println(byClass_vi_e.ownText());
				// stbVN.append(byClass_vi_e.ownText());
				// stbVN.append("\t");
				// }
				// for (int i1 = 0; i1 < byClass.size(); i1++) {
				// stbKethop.append(byClass.get(i1).text() + "\t");
				// // stbVN.append("\t");
				// stbKethop.append(byClass_vi.get(i1 + 1).text() + "\n");
				// // stbVN.append("\n");
				// }

			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		// System.out.println(stbEN.toString());
		// System.out.println(stbVN.toString());
		System.out.println(stbKethop.toString());

	}
}
